cmake_minimum_required(VERSION 3.5)
project(simple_stack_demo C)

# add include directory
include_directories(${CMAKE_SOURCE_DIR}/include)

# gather sources
file(GLOB NET_SRCS "${CMAKE_SOURCE_DIR}/src/net/*.c")
file(GLOB DRV_SRCS "${CMAKE_SOURCE_DIR}/src/drivers/*.c")
set(EXAMPLE_SRCS "${CMAKE_SOURCE_DIR}/examples/demo.c")

set(ALL_SRCS ${NET_SRCS} ${DRV_SRCS} ${EXAMPLE_SRCS})

add_executable(simple_stack_demo ${ALL_SRCS})

if(WIN32)
    # Try project-local Npcap SDK locations first (project root / third_party / deps)
    set(NPCAP_ROOTS
        "${CMAKE_SOURCE_DIR}/npcap"
        "${CMAKE_SOURCE_DIR}/Npcap"
        "${CMAKE_SOURCE_DIR}/third_party/npcap"
        "${CMAKE_SOURCE_DIR}/deps/npcap"
        "${CMAKE_SOURCE_DIR}/third_party/Npcap"
        "${CMAKE_SOURCE_DIR}/deps/Npcap"
    )
    set(NPCAP_INC "")
    set(NPCAP_LIB_DIR "")
    foreach(r ${NPCAP_ROOTS})
        if(EXISTS "${r}/Include/pcap.h")
            set(NPCAP_INC "${r}/Include")
        endif()
        # try common lib layouts
        if(EXISTS "${r}/Lib/x64")
            set(NPCAP_LIB_DIR "${r}/Lib/x64")
        elseif(EXISTS "${r}/Lib/x86")
            set(NPCAP_LIB_DIR "${r}/Lib/x86")
        elseif(EXISTS "${r}/Lib/Win32")
            set(NPCAP_LIB_DIR "${r}/Lib/Win32")
        elseif(EXISTS "${r}/Lib")
            set(NPCAP_LIB_DIR "${r}/Lib")
        endif()
        if(NPCAP_INC AND NPCAP_LIB_DIR)
            break()
        endif()
    endforeach()

    if(NPCAP_INC)
        target_include_directories(simple_stack_demo PRIVATE "${NPCAP_INC}")
    else()
        # fallback: try common Program Files locations
        if(EXISTS "C:/Program Files/Npcap/Include/pcap.h")
            target_include_directories(simple_stack_demo PRIVATE "C:/Program Files/Npcap/Include")
        elseif(EXISTS "C:/Program Files (x86)/Npcap/Include/pcap.h")
            target_include_directories(simple_stack_demo PRIVATE "C:/Program Files (x86)/Npcap/Include")
        endif()
    endif()

    if(NPCAP_LIB_DIR)
        # add lib dir found in project SDK to link directories
        target_link_directories(simple_stack_demo PRIVATE "${NPCAP_LIB_DIR}")
    endif()

    # link common windows libs (wpcap and Packet are typical; ws2_32 required for winsock)
    target_link_libraries(simple_stack_demo PRIVATE wpcap Packet ws2_32)
else()
    find_package(PkgConfig QUIET)
    if(PKG_CONFIG_FOUND)
        pkg_check_modules(PCAP libpcap QUIET)
        if(PCAP_LIBRARIES)
            target_include_directories(simple_stack_demo PRIVATE ${PCAP_INCLUDE_DIRS})
            target_link_libraries(simple_stack_demo PRIVATE ${PCAP_LIBRARIES})
        else()
            target_link_libraries(simple_stack_demo PRIVATE pcap)
        endif()
    else()
        target_link_libraries(simple_stack_demo PRIVATE pcap)
    endif()
endif()

set_target_properties(simple_stack_demo PROPERTIES
    RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin"
)
